import { Injectable, ChangeDetectorRef } from "@angular/core";
import { MatPaginatorIntl } from "@angular/material";
import { LocaleService, TranslationService, Language } from "angular-l10n";
import { Direction } from "@angular/cdk/bidi";

@Injectable({
  providedIn: "root"
})
export class MatPaginatorIntlService extends MatPaginatorIntl {
  itemsPerPageLabel = "";
  nextPageLabel = "Slijedeća stranica";
  previousPageLabel = "Prethodna stranica";
  of = "of";

  dir: Direction;

  constructor(
    public translation: TranslationService,
    public locale: LocaleService
  ) {
    super();
    this.translation.translationChanged().subscribe(() => {
      this.itemsPerPageLabel = this.translation.translate(
        "mat_page_itemsPerPageLabel"
      );
      this.of = this.translation.translate("mat_page_of");
      this.dir = this.locale.getLanguageDirection() as Direction;
      this.changes.next();
    });
  }

  getRangeLabel = function(page, pageSize, length) {
    if (length === 0 || pageSize === 0) {
      return `0 ${this.of} ` + length;
    }
    length = Math.max(length, 0);
    const startIndex = page * pageSize;
    // If the start index exceeds the list length, do not try and fix the end index to the end.
    const endIndex =
      startIndex < length
        ? Math.min(startIndex + pageSize, length)
        : startIndex + pageSize;
    return startIndex + 1 + " - " + endIndex + ` ${this.of} ` + length;
  };
}
